{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import xarray as xr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import time, calendar\n",
    "import pandas as pd\n",
    "\n",
    "from src.config import *\n",
    "from src.constants import *\n",
    "from src.data.poloniex import Poloniex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "polo = Poloniex()\n",
    "\n",
    "start = datetime(2020, 1, 20, 3, 0)\n",
    "end= datetime(2020, 1, 20, 4, 20)\n",
    "\n",
    "start = int(time.mktime(start.timetuple()) - time.timezone)\n",
    "end= int(calendar.timegm(end.timetuple()))\n",
    "\n",
    "period = FIFTEEN_MINUTES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.data.globaldatamatrix import HistoryManager\n",
    "mgr = HistoryManager(coin_number=4, end = end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "panel = mgr.get_global_panel(start=start, end=end, period=period, features=['close', 'high', 'low'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ETH'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coin = panel.coins[3].values\n",
    "str(coin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>quoteVolume</th>\n",
       "      <th>weightedAverage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-20 03:00:00</td>\n",
       "      <td>0.019132</td>\n",
       "      <td>0.019110</td>\n",
       "      <td>0.019110</td>\n",
       "      <td>0.019127</td>\n",
       "      <td>0.292989</td>\n",
       "      <td>15.321845</td>\n",
       "      <td>0.019122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-20 03:15:00</td>\n",
       "      <td>0.019135</td>\n",
       "      <td>0.019119</td>\n",
       "      <td>0.019127</td>\n",
       "      <td>0.019119</td>\n",
       "      <td>1.425917</td>\n",
       "      <td>74.552350</td>\n",
       "      <td>0.019126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-20 03:30:00</td>\n",
       "      <td>0.019131</td>\n",
       "      <td>0.019119</td>\n",
       "      <td>0.019123</td>\n",
       "      <td>0.019128</td>\n",
       "      <td>1.392732</td>\n",
       "      <td>72.818979</td>\n",
       "      <td>0.019126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-20 03:45:00</td>\n",
       "      <td>0.019135</td>\n",
       "      <td>0.019103</td>\n",
       "      <td>0.019130</td>\n",
       "      <td>0.019111</td>\n",
       "      <td>1.781537</td>\n",
       "      <td>93.159341</td>\n",
       "      <td>0.019124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-20 04:00:00</td>\n",
       "      <td>0.019148</td>\n",
       "      <td>0.019089</td>\n",
       "      <td>0.019113</td>\n",
       "      <td>0.019111</td>\n",
       "      <td>3.112476</td>\n",
       "      <td>162.720579</td>\n",
       "      <td>0.019128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2020-01-20 04:15:00</td>\n",
       "      <td>0.019113</td>\n",
       "      <td>0.019075</td>\n",
       "      <td>0.019109</td>\n",
       "      <td>0.019078</td>\n",
       "      <td>0.322440</td>\n",
       "      <td>16.883259</td>\n",
       "      <td>0.019098</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 date      high       low      open     close    volume  \\\n",
       "0 2020-01-20 03:00:00  0.019132  0.019110  0.019110  0.019127  0.292989   \n",
       "1 2020-01-20 03:15:00  0.019135  0.019119  0.019127  0.019119  1.425917   \n",
       "2 2020-01-20 03:30:00  0.019131  0.019119  0.019123  0.019128  1.392732   \n",
       "3 2020-01-20 03:45:00  0.019135  0.019103  0.019130  0.019111  1.781537   \n",
       "4 2020-01-20 04:00:00  0.019148  0.019089  0.019113  0.019111  3.112476   \n",
       "5 2020-01-20 04:15:00  0.019113  0.019075  0.019109  0.019078  0.322440   \n",
       "\n",
       "   quoteVolume  weightedAverage  \n",
       "0    15.321845         0.019122  \n",
       "1    74.552350         0.019126  \n",
       "2    72.818979         0.019126  \n",
       "3    93.159341         0.019124  \n",
       "4   162.720579         0.019128  \n",
       "5    16.883259         0.019098  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chart = polo.marketChart(period=period, start=start, end=end, pair = 'BTC_' + str(coin))\n",
    "chart = pd.DataFrame(chart)\n",
    "chart.date = pd.to_datetime(chart.date, unit = 's')\n",
    "chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = panel.sel(coins='ETH', features='low').values[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "ch = chart.low.values[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ch == db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Cash(tf.keras.Model):\n",
    "\n",
    "    def __init__(self):\n",
    "        super(Cash, self).__init__()\n",
    "\n",
    "        self.dense = tf.keras.layers.Dense(5, activation='relu')\n",
    " \n",
    "        self.b = tf.Variable(tf.zeros((1, 1), dtype=tf.float32), trainable=True)\n",
    "        self.out = tf.keras.layers.Activation('softmax')\n",
    "\n",
    "    def call(self, x):\n",
    "        x = self.dense(x)\n",
    "        bias = tf.tile(self.b, [tf.shape(x)[0], 1])\n",
    "        x = tf.concat((bias, x), axis = -1)\n",
    "        x = self.out(x)\n",
    "        \n",
    "        return x\n",
    "    \n",
    "    \n",
    "model = Cash()\n",
    "\n",
    "X = np.random.randn(100, 3)\n",
    "with tf.GradientTape() as tape:\n",
    "    y = model(X)\n",
    "\n",
    "print([var.name for var in tape.watched_variables()])\n",
    "grads = tape.gradient(y, model.trainable_variables)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Cash(tf.keras.Model):\n",
    "\n",
    "    def __init__(self, rows = 11, cols = 50, features = 3):\n",
    "        super(Cash, self).__init__()\n",
    "        \n",
    "        input_shape = (rows, cols, features)\n",
    "        inputs = tf.keras.Input(shape=input_shape)\n",
    "        x = tf.keras.layers.Conv2D(\n",
    "            filters = 2, \n",
    "            kernel_size = (1,3), \n",
    "            padding='valid', \n",
    "            activation='relu',\n",
    "            name = 'conv1'\n",
    "        )(inputs)\n",
    "        x = tf.keras.layers.Conv2D(1, \n",
    "                                (1, x.shape[2]), \n",
    "                                activation=\"relu\", \n",
    "                                name = 'conv2')(x)\n",
    "        x = tf.squeeze(x)\n",
    "        \n",
    "        self.model = tf.keras.Model(inputs = inputs, outputs = x)\n",
    "        \n",
    "        self.b = tf.Variable(tf.zeros((1, 1), dtype=tf.float32), trainable=True)\n",
    "        self.out = tf.keras.layers.Activation('softmax')\n",
    "\n",
    "    def call(self, x):\n",
    "        x = self.model(x)\n",
    "        print(x.shape)\n",
    "        bias = tf.tile(self.b, [tf.shape(x)[0], 1])\n",
    "        x = tf.concat((bias, x), axis = -1)\n",
    "        x = self.out(x)\n",
    "        \n",
    "        return x\n",
    "    \n",
    "    \n",
    "model = Cash()\n",
    "\n",
    "X = np.random.randn(100, 11, 50, 3)\n",
    "with tf.GradientTape() as tape:\n",
    "    y = model(X)\n",
    "\n",
    "print([var.name for var in tape.watched_variables()])\n",
    "grads = tape.gradient(y, model.trainable_variables)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch = 100\n",
    "dense = tf.keras.layers.Dense(5, activation='relu')\n",
    "b = tf.Variable(tf.zeros((1,1), dtype=tf.float32), name='b', trainable=True)\n",
    "\n",
    "x =  tf.random.normal((batch, 3))\n",
    "with tf.GradientTape() as tape:\n",
    "    y = dense(x)\n",
    "    cash = tf.tile(b, [y.shape[0], 1])\n",
    "    y = tf.concat((cash, y), axis = -1)\n",
    "    y = tf.keras.layers.Activation('softmax')(y)\n",
    "\n",
    "print([var.name for var in tape.watched_variables()])\n",
    "grads = tape.gradient(y, [b, dense.trainable_variables])\n",
    "grads"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def CNN(rows, cols, features, batch_size):\n",
    "    input_shape = (rows, cols, features)\n",
    "    X = keras.Input(shape= input_shape, batch_size=batch_size)\n",
    "    w = keras.Input(shape = (rows, 1, 1), batch_size=batch_size)\n",
    "\n",
    "    x = tf.keras.layers.Conv2D(\n",
    "        filters = 2, \n",
    "        kernel_size = (1,3), \n",
    "        padding='valid', \n",
    "        activation='relu'\n",
    "    )(X)\n",
    "        \n",
    "    x = keras.layers.Conv2D(20, \n",
    "                            (1, x.shape[2]), \n",
    "                            activation=\"relu\", \n",
    "                            name = 'conv2')(x)  \n",
    "    \n",
    "    con = keras.layers.Concatenate(axis=3)([x, w])\n",
    "\n",
    "    x = keras.layers.Conv2D(1, (1,1), name = 'votes')(con)\n",
    "    x = tf.squeeze(x)\n",
    "    \n",
    "    b = tf.tile(b, [x.shape[0], 1])\n",
    "    with_bias = keras.layers.Concatenate(axis=1)([b, x])\n",
    "    #with_bias = CashBias()(x)\n",
    "\n",
    "    outputs = keras.layers.Activation('softmax')(with_bias)\n",
    "    return keras.Model(inputs = [X, w], outputs = outputs, name = \"Policy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import layers\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs = keras.Input(shape=(784,), name=\"digits\")\n",
    "x1 = layers.Dense(64, activation=\"relu\")(inputs)\n",
    "x2 = layers.Dense(64, activation=\"relu\")(x1)\n",
    "outputs = layers.Dense(10, name=\"predictions\")(x2)\n",
    "model = keras.Model(inputs=inputs, outputs=outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Instantiate an optimizer.\n",
    "optimizer = keras.optimizers.SGD(learning_rate=1e-3)\n",
    "# Instantiate a loss function.\n",
    "loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prepare the training dataset.\n",
    "batch_size = 64\n",
    "(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
    "x_train = np.reshape(x_train, (-1, 784))\n",
    "x_test = np.reshape(x_train, (-1, 784))\n",
    "train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))\n",
    "train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (x, y) in train_dataset:\n",
    "\n",
    "    with tf.GradientTape() as tape:\n",
    "\n",
    "        # Run the forward pass of the layer.\n",
    "        # The operations that the layer applies\n",
    "        # to its inputs are going to be recorded\n",
    "        # on the GradientTape.\n",
    "        logits = model(x, training=True)  # Logits for this minibatch\n",
    "\n",
    "        # Compute the loss value for this minibatch.\n",
    "        loss_value = loss_fn(y, logits)\n",
    "\n",
    "    # Use the gradient tape to automatically retrieve\n",
    "    # the gradients of the trainable variables with respect to the loss.\n",
    "    grads = tape.gradient(loss_value, model.trainable_weights)\n",
    "\n",
    "    # Run one step of gradient descent by updating\n",
    "    # the value of the variables to minimize the loss.\n",
    "    # optimizer.apply_gradients(zip(grads, model.trainable_weights))\n",
    "    shapes = [grad.shape for grad in grads]\n",
    "    print(shapes)\n",
    "    print(model.trainable_variables)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [np.random.rand(2,3) for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.array(a, dtype='float32').dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
